<template>
	<view class="average">
		<custom-navi :show-home="false">消费设置<view class="putin" @click="record">提交</view></custom-navi>
		<view class="average-set">
			<view v-for="(item,index) in averageData" :key='index' :class="{sign:sign==index}" @click="signBtn(index)">
				{{item.txt}}
			</view>
		</view>
		<view class="average-input">
			<input type="text" :value="price" placeholder="请输入金额" @input="moneyBtn"/>
		</view>
	</view>
</template>

<script>
	import {
	  updatePerCapitaPrice
	} from '../../api'
	export default {
		data(){
			return {
				averageData:[
					{txt:'人均消费'},
					{txt:'最低起消费'},
				],
				sign:'',
				money:'',//人均消费
				moneys:'',//最低起消费
				price:'',
				shopInfo: {},
			}
		},
		onLoad(){
			this.sign=0
			const _self = this
			uni.getStorage({
			  key: 'shopInfo',
			  success: function (res) {
			    _self.shopInfo = res.data
			  }
			});
			console.log(_self.shopInfo)
			if(_self.shopInfo){
				this.money=_self.shopInfo.perCapitaPrice
				this.moneys=_self.shopInfo.minPrice
				this.price=this.money
			}
		},
		methods:{
			signBtn(index){
				this.sign=index
				if(this.sign==0){
					this.price=this.money
				}else{
					this.price=this.moneys
				}
			},
			moneyBtn(e){
				// console.log(e)
				if(this.sign==0){
					this.money=e.detail.value
					this.price=this.money
				}else{
					this.moneys=e.detail.value
					this.price=this.moneys
				}
			},
			
					async record(){
						let params = {
						  id:this.shopInfo.id,
						  minPrice:this.moneys,
						  perCapitaPrice:this.money
						}
						if(this.money==''){
							this.$utils.common.toast.fail('请输入人均消费');
						}else if(this.moneys==''){
							this.$utils.common.toast.fail('请输入最低起消费');
						}else{
						try {
						  const {
						    data
						  } = await updatePerCapitaPrice(params)
						
						 // console.log(data)
						 this.$utils.common.toast.fail("修改成功");
						setTimeout(() => {
						  return this.$utils.common.navigateBack()
						}, 1000)
						 
						} catch (err) {
						  console.log(err)
						  if (err.code && err.statusCode) {
						    if (err.code !== 200) {
						      return this.$utils.common.toast.fail(err.data.message);
						    }
						    return this.$utils.common.toast.fail(err.errMsg);
						  }
						}
					}
				}
			
		}
	}
</script>

<style lang="scss" scoped>
	.putin{
		// height: 23rpx;
		line-height: 44rpx;
		  position: absolute;
		  top: 0;
		  right: 32rpx;
		  font-size:30rpx;
		  font-family:PingFang SC;
		  font-weight:500;
		  color:rgba(255,255,255,1);
		  opacity:1;
		  
	}
	.average{
		width: 100%;
		&-input{
			width:632rpx;
			height:86rpx;
			
			background:rgba(240,245,249,1);
			border:2rpx solid rgba(12,212,211,1);
			opacity:1;
			// line-height: 86rpx;
			border-radius:20rpx;
			margin: 0 auto;
			display: flex;
			align-items: center;
			padding-left: 40rpx;
			input{
				
			}
		}
		&-set{
			display: flex;
			padding: 42rpx 50rpx;
			.sign{
				background:rgba(0,213,212,1);
				border:2rpx solid rgba(0,213,212,1);
				font-size:28rpx;
				font-family:PingFang SC;
				font-weight:400;
				color:rgba(255,255,255,1);
				opacity:1;
			}	
			view{
				width:310rpx;
				height:80rpx;
				font-family:PingFang SC;
				font-weight:400;
				font-size:28rpx;
				color:rgba(92, 92, 92, 1);
				background:rgba(255,255,255,1);
				border:2rpx solid rgba(0,213,212,1);
				opacity:1;
				line-height: 80rpx;
				text-align: center;
				border-radius:20rpx;
				margin-right: 24rpx;
			}
			view:last-child{
				margin-right:0rpx
			}
		}
	}
</style>
